package com.vast.ui.component.providers;

import org.eclipse.jface.viewers.CellEditor;
import org.eclipse.jface.viewers.CheckboxCellEditor;
import org.eclipse.jface.viewers.ColumnViewer;
import org.eclipse.jface.viewers.EditingSupport;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TextCellEditor;
import org.eclipse.swt.SWT;

import com.vast.ui.component.models.Item;
import com.vast.ui.component.models.ItemArray;

public class ItemEditingSupport extends EditingSupport {
	private CellEditor editor;
	private final int columnIndex;
	
	public ItemEditingSupport(ColumnViewer viewer, int columnIndex) {
		super(viewer);
		// TODO Auto-generated constructor stub
		this.editor = new CheckboxCellEditor(null, SWT.CHECK | SWT.READ_ONLY);
		this.columnIndex = columnIndex;
	}

	@Override
	protected boolean canEdit(Object element) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	protected CellEditor getCellEditor(Object element) {
		// TODO Auto-generated method stub
		return editor;
	}

	@Override
	protected Object getValue(Object element) {
		// TODO Auto-generated method stub
		ItemArray itemArray = (ItemArray)element;
		Item      item      = itemArray.getItem(columnIndex);
		return    item.getValueAsHexString();
	}

	@Override
	protected void setValue(Object element, Object value) {
		// TODO Auto-generated method stub
		//if(columnIndex != 0) {
			ItemArray itemArray  = (ItemArray)element;
			Item      item       = itemArray.getItem(columnIndex);
		          item.value = (int)(Long.decode("0x" + value)& 0xFFFFFFFF);     
		    getViewer().update(element, null);
		//}
	}

}

